package com.eat.bluetooth2;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "Apple";
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothReceiver bluetoothReceiver;
    private Button scanButton,discoverButton;
    private TextView mainLv;
    private String M = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainLv = (TextView) findViewById(R.id.textView);

        discoverButton = (Button) findViewById(R.id.discoverButtonId);
        discoverButton.setOnClickListener(new DiscoverButtonListener());

        bluetoothReceiver = new BluetoothReceiver();
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(bluetoothReceiver, intentFilter);

        intentFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(bluetoothReceiver, intentFilter);

        scanButton = (Button) findViewById(R.id.scanBluetoothId);
        Log.d(TAG, ""+ scanButton);
        scanButton.setOnClickListener(new ScanButtonListener());
    }

    private class BluetoothReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    String deviceName = device.getName();
                    String deviceAddress = device.getAddress();
                    String Msg = "name:"+deviceName+",address:"+deviceAddress+'\n';
                    Log.d(TAG, Msg);
                    M +=Msg;
                    mainLv.setText(M);
                }
                else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
                        .equals(action)) {
                    bluetoothAdapter.cancelDiscovery();
                }
            }
        }
    }

    private class ScanButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            bluetoothAdapter.startDiscovery();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private class DiscoverButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
            startActivity(discoverIntent);
        }
    }
}
